/**
 * @projectName demo-master
 * @fileName TestController.java
 * @packageName org.example.customer.controller
 * @author xaoyaoyao
 * @date 2021/3/30 10:25
 * @version V1.0
 * @copyright (c) 2021, xaoyaoyao@aliyun.com All Rights Reserved.
 */
package org.example.customer.controller;

import feign.Feign;
import feign.jackson.JacksonDecoder;
import feign.jackson.JacksonEncoder;
import feign.okhttp.OkHttpClient;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.example.customer.feign.Contributor;
import org.example.customer.feign.CustomerClient;
import org.example.customer.feign.HelloWorld;
import org.example.customer.util.FeignUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author xaoyaoyao
 * @className TestController
 * @description
 * @date 2021/3/30 10:25
 */
@Slf4j
@RestController
@RequestMapping("/customer")
public class TestController {

	@Value("${feign.client.url}")
	private String baseUrl;

	@GetMapping("/testFeignApi")
	public String testFeignApi(@RequestParam(value = "param") String param) {
		CustomerClient target = Feign.builder()
				.client(new OkHttpClient())
				.encoder(new JacksonEncoder())
				.decoder(new JacksonDecoder())
				.target(CustomerClient.class, baseUrl);
		HelloWorld helloWorld = target.testFeignProvider(param);
		log.debug("helloWorld >> {}", helloWorld);
		return "hello World ! ";
	}

	@PostMapping("/testFeignProviderHasParameter/{id}")
	public HelloWorld testFeignProviderHasParameter(@PathVariable String id) {
		CustomerClient target = Feign.builder()
				.client(new OkHttpClient())
				.encoder(new JacksonEncoder())
				.decoder(new JacksonDecoder())
				.target(CustomerClient.class, baseUrl);
		HelloWorld helloWorld = target.testFeignProviderHasParameter(id);
		log.debug("helloWorld >> ", helloWorld);
		return helloWorld;
	}

	@PostMapping("/testFeignApiHasParameterC")
	public String testFeignApiHasParameter() {
		CustomerClient target = Feign.builder()
				.client(new OkHttpClient())
				.encoder(new JacksonEncoder())
				.decoder(new JacksonDecoder())
				.target(CustomerClient.class, baseUrl);
		HelloWorld helloWorld = target.testFeignProviderHasParameter("OpenFeign");
		return "hello World ! 我是另一个服务返回的结果哦！！！" + helloWorld;
	}

	@PostMapping("/testFeignApiHasParameterB")
	public String testFeignApiHasParameterBJson() {
		CustomerClient send       = new FeignUtil<CustomerClient>().send(CustomerClient.class);
		HelloWorld     helloWorld = new HelloWorld();
		helloWorld.setParam("hello nacos feign");
		HelloWorld hasParameterBJson = send.testFeignApiHasParameterBJson(helloWorld);
		return "hello World ! 我是另一个服务返回的结果哦！！！" + hasParameterBJson;
	}

	@PostMapping(value = "/testFeignApiHasParameterBJson", consumes = { MediaType.APPLICATION_JSON_VALUE })
	@ResponseBody
	public HelloWorld testFeignApiHasParameterBJson2(@RequestBody HelloWorld helloWorld) {
		CustomerClient send              = new FeignUtil<CustomerClient>().send(CustomerClient.class);
		HelloWorld     hasParameterBJson = send.testFeignApiHasParameterBJson2(helloWorld);
		return hasParameterBJson;
	}

	@GetMapping(value = "/contributors")
	public List<Contributor> contributors(@RequestParam("owner") String owner, @RequestParam("repo") String repo) {
		CustomerClient target = Feign.builder()
				.client(new OkHttpClient())
				.encoder(new JacksonEncoder())
				.decoder(new JacksonDecoder())
				.target(CustomerClient.class, baseUrl);
		List<Contributor> list = target.contributors(owner, repo);
		log.debug("list >> {}", list);
		return list;
	}
}
